<template>
  <div class="header">
    <div class="timeBox">
      <p>{{day}}</p>
      <p>{{month}}月</p>
    </div>
    <p class="h_title">知乎日报</p>
    <a @click="loginOrUser">
      <img class="userImg" v-if="!isLogin" src="./../../src/public/imges/noLoginuser.png" alt />
      <img class="userImg" v-else :src="user.userImg" alt />
    </a>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  data: function () {
    return {
      month: "",
      day: "",
    };
  },
  computed: {
    ...mapState("login", ["isLogin", "user"]),
  },
  created() {
    var date = new Date();
    this.month = this.toChinesNum(date.getMonth() + 1);
    this.day = date.getDate();
    // console.log(this.month, this.day);
  },
  methods: {
    toChinesNum(num) {
      //数字转汉字
      let changeNum = [
        "零",
        "一",
        "二",
        "三",
        "四",
        "五",
        "六",
        "七",
        "八",
        "九",
      ];
      let unit = ["", "十", "百", "千", "万"];
      num = parseInt(num);
      let getWan = (temp) => {
        let strArr = temp.toString().split("").reverse();
        let newNum = "";
        for (var i = 0; i < strArr.length; i++) {
          newNum =
            (i == 0 && strArr[i] == 0
              ? ""
              : i > 0 && strArr[i] == 0 && strArr[i - 1] == 0
              ? ""
              : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) +
            newNum;
        }
        return newNum;
      };
      let overWan = Math.floor(num / 10000);
      let noWan = num % 10000;
      if (noWan.toString().length < 4) {
        noWan = "0" + noWan;
      }
      return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
    },
    loginOrUser() {
      if (this.isLogin) {
        this.$router.push("/user");
      } else {
        this.$router.push("/login");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.header {
  height: 0.5rem;
  border-bottom: 1px solid #ccc;
  display: flex;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  background: #fff;
  z-index: 9999;
}
.timeBox {
  padding: 0 0.1rem;
  margin: 0.05rem 0.1rem 0.05rem 0;
  border-right: 0.01rem solid #aaa;
  text-align: center;
  p {
    font-size: 0.12rem;
  }
  p:nth-child(1) {
    font-weight: 700;
    font-size: 0.22rem;
  }
}
.h_title {
  flex: 1;
  font-weight: 700;
  font-size: 0.24rem;
  line-height: 0.5rem;
}
.userImg {
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
  margin: 0.05rem 0.05rem 0 0;
}
</style>